package com.lanou.zy.projecta.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.lanou.zy.projecta.R;
import com.lanou.zy.projecta.entity.Question;
import com.lanou.zy.projecta.util.DataChange;

import java.util.List;

/**
 * Created by dllo on 15/11/4.
 */
public class QuestionRecyclerViewAdapter extends RecyclerView.Adapter<QuestionRecyclerViewAdapter.QuestionViewHolder>{
    private List<Question.DataEntity> entities;

    public void addData(List<Question.DataEntity> datas) {
        this.entities = datas;
        notifyDataSetChanged();
    }

    @Override
    public QuestionViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) {
        LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
        View view = inflater.inflate(R.layout.question_recyclerview_item, null);
        return new QuestionViewHolder(view);
    }

    @Override
    public void onBindViewHolder(QuestionViewHolder holder, int position) {
        if (entities != null && entities.size() > 0) {
            Question.DataEntity qd = entities.get(position);
            holder.questionUsersName_Tv.setText(qd.getAuthor());
            holder.questionAnswer_Tv.setText("同问" + qd.getAnswer_num());
            holder.questionContent_Tv.setText(qd.getContent());
            holder.questionTime_Tv.setText(DataChange.getStandardDate(qd.getAdd_time()));
            holder.questionTitle_Tv.setText(qd.getTitle());
            holder.questionAsk_Tv.setText("回答" + qd.getAsk_num());
        }

    }

    @Override
    public int getItemCount() {
        return entities != null && entities.size() > 0 ? entities.size() : 0;
    }

    public class QuestionViewHolder extends RecyclerView.ViewHolder {
        private TextView questionTitle_Tv, questionContent_Tv, questionUsersName_Tv, questionTime_Tv, questionAsk_Tv, questionAnswer_Tv;

        public QuestionViewHolder(View v) {
            super(v);
            questionAsk_Tv = (TextView) v.findViewById(R.id.questionAsk_Tv);
            questionTime_Tv = (TextView) v.findViewById(R.id.questionTime_Tv);
            questionTitle_Tv = (TextView) v.findViewById(R.id.questionTitle_Tv);
            questionAnswer_Tv = (TextView) v.findViewById(R.id.questionAnswer_Tv);
            questionContent_Tv = (TextView) v.findViewById(R.id.questionContent_Tv);
            questionUsersName_Tv = (TextView) v.findViewById(R.id.questionUsersName_Tv);
        }
    }
}
